//
//  TTShopListViewController.m
//  TaoTuo
//
//  Created by LG on 2020/8/16.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTShopListViewController.h"

#import "TTExtractAddressViewCell.h"

static NSString *cellID = @"cell_id";

@interface TTShopListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, copy) void(^selectBlock)(TTSendStoreListModel *model);

@property (nonatomic, strong) NSIndexPath *lastIndexPath;

@property (nonatomic, strong) TTSendStoreListModel *selectModel;

@end

@implementation TTShopListViewController

- (instancetype)initWithShopListArray:(NSArray *)shopArray selectBlock:(void (^)(TTSendStoreListModel * _Nonnull))selectBlock
{
    if (self == [super init]) {
        [self.dataArray addObjectsFromArray:shopArray];
        self.selectBlock = selectBlock;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"提货地址";
    
    for (TTSendStoreListModel *model in self.dataArray) {
        if ([model.storeName isEqualToString:self.listModel.storeName]) {
            model.isSelect = YES;
        } else {
            model.isSelect = NO;
        }
    }
    
    [self.tableView reloadData];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TTExtractAddressViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.listModel = self.dataArray[indexPath.row];
    @weakify(self)
    cell.selectStoreListBlock = ^(TTExtractAddressViewCell * _Nonnull tCell, TTSendStoreListModel * _Nonnull model) {
        @strongify(self)
        NSIndexPath *inp = [tableView indexPathForCell:tCell];
        [self handelClickCellOrBtnWithIndexPath:inp];
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self handelClickCellOrBtnWithIndexPath:indexPath];
    
}

- (void)handelClickCellOrBtnWithIndexPath:(NSIndexPath *)indexPath
{
    if (self.lastIndexPath) {
        TTSendStoreListModel *addrModel = self.dataArray[self.lastIndexPath.row];
        addrModel.isSelect = NO;
    }
    
    TTSendStoreListModel *model = self.dataArray[indexPath.row];
    model.isSelect = YES;
    
    self.lastIndexPath = indexPath;
    
    self.selectModel = model;
    
    if (self.selectBlock) {
        self.selectBlock(model);
    }
    
    [self.tableView reloadData];
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = kSeparatorColor;
        _tableView.tableFooterView = [UIView new];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.rowHeight = 66;
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([TTExtractAddressViewCell class]) bundle:nil] forCellReuseIdentifier:cellID];
        [self.view addSubview:_tableView];
        
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.mas_topLayoutGuideTop);
            make.left.right.equalTo(self.view);
            make.bottom.equalTo(self.mas_bottomLayoutGuideBottom);
        }];
    }
    return _tableView;
}


- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


@end
